package net.xuele.learn.dancing.tree;

import net.xuele.learn.dancing.Node;
import net.xuele.learn.dancing.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 *
 *
 *
 */
public class Solution {
    List<Integer> result = new ArrayList<>();

    /**
     * N叉树的前序遍历
     *
     * @param root
     * @return
     */
    public List<Integer> preorder(Node root) {
        if (root == null) {
            return result;
        }
        result.add(root.val);
        for (Node child : root.children) {
            preorder(child);
        }
        return result;
    }

    /**
     * 94 二叉树中序遍历
     * 二叉树的中序遍历
     *
     * @param root
     * @return
     */
    public List<Integer> inorderTraversal(TreeNode root) {
        if (root == null) {
            return result;
        }
        result = inorderTraversal(root.left);
        result.add(root.val);
        result = inorderTraversal(root.right);
        return result;
    }

    /**
     * 897 递增顺序查找树
     *
     * @param root
     * @return
     */

    static List<List<Integer>> res = new LinkedList<>();

    public static List<List<Integer>> combine(int n, int k) {
        LinkedList<Integer> result = new LinkedList<>();
        backtrace(n, k, 1, result);
        return res;
    }

    private static void backtrace(int n, int k, int begin, LinkedList<Integer> track) {
        // 触发结束条件：nums 中的元素全都在 track 中出现
        if (track.size() == k) {
            res.add(new LinkedList<>(track));
            return;
        }
        for (int i = begin; i < n + 1; i++) {
            track.add(i);
            // 进入下一层决策树
            backtrace(n, k, i + 1, track);
            // 取消选择
            track.removeLast();
        }
    }

    public static void main(String[] args) {
        combine(4, 2);
    }

}
